package main

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"log"
	"short_video/api"
	"short_video/config"
	"short_video/consts"
	"short_video/inits"
	"short_video/internal/dao"
	"short_video/internal/model"
	"short_video/queue"
)

func main() {
	config.InitsConfig()
	inits.InitMysql()
	inits.InitRedis()
	inits.InitRabbitMq()
	go MQ()
	router := gin.Default()
	//router.GET("/ping", func(c *gin.Context) {
	//	c.JSON(200, gin.H{
	//		"message": "pong",
	//	})
	//})
	api.ToadRouter(router)
	router.Run(":8519") // 监听并在 0.0.0.0:8080 上启动服务
}
func MQ() {

	msgs := queue.QueueSub(consts.Queue_Pub) //QueuePub

	forever := make(chan bool)
	//启用协程处理消息
	go func() {
		for d := range msgs {
			//消息逻辑处理，可以自行设计逻辑
			log.Printf("Received a message: %s", d.Body)
			var data model.ChatHistory
			json.Unmarshal(d.Body, &data)
			dao.Create(&data)
		}
	}()

	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever

	//msgs := queue.PublishSub(consts.Queue_Pub)
	//
	//forever := make(chan bool)
	////启用协程处理消息
	//go func() {
	//	for d := range msgs {
	//		//消息逻辑处理，可以自行设计逻辑
	//		log.Printf("Received a message: %s", d.Body)
	//		var data model.ChatHistory
	//		json.Unmarshal(d.Body, &data)
	//		dao.Create(&data)
	//	}
	//}()
	//
	//log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	//<-forever
}
